<!DOCTYPE html>
<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
	contentType="text/html;charset=utf-8"%>
<html>
<head>
<title>行政区划管理-新建行政区划</title>
<jsp:include page="/common/head.jsp"></jsp:include>
<link
	href="<%=request.getContextPath()%>/static/plugin/zTree/css/zTreeStyle/zTreeStyle.css"
	rel="stylesheet">
<script
	src="<%=request.getContextPath()%>/static/plugin/zTree/js/jquery.ztree.all-3.5.min.js"></script>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>行政区划管理</span>
            <span class="split"></span>
            <span>新建行政区划</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

	<form id="regionForm">
		<div class="form-wrapper">
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>上级节点</p>
             <input type="hidden" name="pId" value="${pId}"/>
             <input type="text" id="parentName" class="summer-input distance-right" value="${parentName}" readonly/>
            <label class="label-secondary "></label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>名称</p>
             <input type="text" id="name" name="name" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required','maxLength[50]']" />
            <label class="label-secondary "></label>
        </div>
        
         <div class="form-cont">
            <p class="form-cont-title">简称</p>
             <input type="text" id="sName" name="sName" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['maxLength[20]']"/>
            <label class="label-secondary "></label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>类别</p>
             <input type="text" id="type" name="type" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required']"/>
            <label class="label-secondary "></label>
        </div>
        
        <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>代码</p>
             <input type="text" id="code" name="code" class="summer-input distance-right" />
            <label class="label-secondary "></label>
        </div>
        
         <div class="form-cont">
            <p class="form-cont-title"><span class="field-required">*</span>排序号</p>
             <input type="text" id="orderNo" name="orderNo" class="summer-input summer-validatebox distance-right" data-validatebox="validType:['required','betweenInteger[1,100000]']"/>
            <label class="label-secondary "></label>
        </div>
        
    </div>
	
	<div class="form-btn-wrapper">
	    <a class="btn btn-normal btn-ok distance-right" id="saveBtn">保存</a>
	    <a class="btn btn-normal btn-ok" id="saveBtn1">保存并继续</a>
	</div>	
	</form>
	
	<div class="callout callout-ok" style="margin-top: 20px;">
		<h4>操作说明</h4>
		<p>1、当前行政区划是省级别时请填写简称</p>
	</div>

	<script>
         
         var REGION_URL = {
        		 ADD: basePath + '/sys/region/add',
                 CHECKEXIST: basePath + "/sys/region/isExist",
                 INIT_STAT: basePath + '/sys/region/initStat'
         },
         level = ${thisLevel};
     
        $(function() {
        	$("#type").combobox({
                valueField: 'code',
                textField:  'name',
                menuHeight: 130,
                url : COMMON_URL.getCode + "?type=T_DM_REGION_TYPE",
                onLoadSuccess: function(){
                	$('#type').combobox("setValue",level); 
                }
            });
        	 // 保存
        	 $('#saveBtn1').click(function(){
                 save(true);
             });
        	 // 保存并继续
             $('#saveBtn').click(function(){
                 save(false);
             })
             // 返回按钮事件
            $('#goBack').on('click',goBack);
             //校验代码code是否可用   
            validateExist('#code','code', REGION_URL.CHECKEXIST, ['required','maxLength[12]','regexp'], "代码可用");
        });
        
        function save(isContinue) {
        	 $("#regionForm").form({
        		 buttons : ['#saveBtn','#saveBtn1'],
                 ajax: {
                     url: REGION_URL.ADD,
                 },
                 onSubmitSuccess: function(data){
                	 $.stip({
                         type:'ok',//显示类型 有info、 ok、 danger
                         text: '保存成功！',
                         onBeforeHide: function () {
                        	 if (isContinue){
                                 parent.refreshPage('${pId}', true, level); 
                                 formReset();
                             } else {
                                 parent.refreshPage('${pId}');
                             }
                        }
                     });
                 }
             }).form("submit");
        }
        // 重置表单内容
        function formReset(){
            $("#name,#sName,#code,#orderNo").val("");
            $('#type').combobox("setValue",level + 1);
        }
        
        //校验代码code是否可用
        function validateExist(id, name, url, validType, message) {
            $(id).validatebox({
                rules: {
                    regexp: {
                        validator: function (value,param) {
                            var result = false,
                                params = {}, 
                                key = name;
                                params[key] = value;
                                
                            $.ajax({
                                type: 'post',
                                url: url,
                                data: params,
                                dataType: 'json',
                                async: false,
                                success: function (data) {
                                	result = !(data.result == 'exist');
                                }
                            });
                            return result;
                        },
                        message: message,
                    }
                },
                validType: validType
            });
        }
        // 返回
        function goBack() {
            window.location.href=REGION_URL.INIT_STAT;
        }
     </script>
</body>
</html>
